<template>
  <div class="container">
    <div>鼠标位置：</div>
    <div>X轴：{{ x }}</div>
    <div>Y轴：{{ y }}</div>
    <hr />
    <div>{{ count }} <button @click="add()">自增</button></div>
    <hr />
    <h3>{{ name }}</h3>
    <button @click="setName">修改name</button>
  </div>
</template>
<script>
//  onMounted, onUnmounted（生命周期）, reactive, ref, toRefs（响应式数据处理）
import { onMounted, onUnmounted, reactive, ref, toRefs } from 'vue'
export default {
  name: 'App',
  setup () {
    // 鼠标移动逻辑
    const mouse = reactive({
      x: 0,
      y: 0
    })
    // x:{value:0},y:{value:0}
    function move (e) {
      mouse.x = e.pageX
      mouse.y = e.pageY
    }
    onMounted(() => {
      document.addEventListener('mousemove', move)
    })
    onUnmounted(() => {
      document.removeEventListener('mousemove', move)
    })

    // 累加逻辑
    const count = ref(0)
    const add = () => {
      count.value++
    }
    // 显示name
    let name = '董浩飞'
    function setName () {
      name = '董老师'
      console.log(name)
    }

    // 返回数据
    return {
      ...toRefs(mouse),
      count,
      add,
      name,
      setName
    }
  }
}
</script>
